<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择人员性别" clearable>
          <el-option
            v-for="dict in sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="领导姓名" prop="leaderName">
        <el-input
          v-model="queryParams.leaderName"
          placeholder="请输入领导姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="peopleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="姓名" align="center" prop="name" show-overflow-tooltip>
            <template #default="scope">
                <span>{{ scope.row.name || "暂无" }}</span>
            </template>
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age" show-overflow-tooltip>
            <template #default="scope">
                <span>{{ scope.row.age || "暂无" }}</span>
            </template>
        </el-table-column>
      <el-table-column label="人员性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="人脸图像" align="center" prop="faceImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.faceImage || ''" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="birthDate" width="180" show-overflow-tooltip>
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
        <el-table-column label="领导姓名" align="center" prop="leaderName" show-overflow-tooltip>
            <template #default="scope">
                <span>{{ scope.row.leaderName || "暂无" }}</span>
            </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="peopleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="人员性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择人员性别">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人脸图像">
          <image-upload v-model="form.faceImage"/>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领导主键" prop="leaderUuid">
          <el-input v-model="form.leaderUuid" placeholder="请输入领导主键" />
        </el-form-item>
        <el-form-item label="领导姓名" prop="leaderName">
          <div style="display: flex;">
            <el-input v-model="form.leaderName" placeholder="请输入领导姓名" />
            <el-button type="primary" @click="handleClickLeader">选择</el-button>
          </div>
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入简介" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="领导选择" v-model="leaderOpen" width="600px" append-to-body>
      <el-table v-loading="loading" :data="peopleList" @selection-change="handleSelectionChange">
        <el-table-column label="姓名" align="center" prop="name" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.name || "暂无" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.age || "暂无" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="人员性别" align="center" prop="sex">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="人脸图像" align="center" prop="faceImage" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.faceImage || ''" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button
                type="text"
                icon="Edit"
                @click="handleSelect(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup name="People">
import { listPeople, getPeople, delPeople, addPeople, updatePeople } from "@/api/system/people";

const { proxy } = getCurrentInstance();
const { sys_user_sex } = proxy.useDict('sys_user_sex');

const peopleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    age: null,
    sex: null,
    faceImage: null,
    birthDate: null,
    leaderUuid: null,
    leaderName: null,
    introduction: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询人员信息列表 */
function getList() {
  loading.value = true;
  listPeople(queryParams.value).then(response => {
    peopleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    uuid: null,
    name: null,
    age: null,
    sex: null,
    faceImage: null,
    birthDate: null,
    leaderUuid: null,
    leaderName: null,
    introduction: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("peopleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.uuid);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加人员信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const uuid = row.uuid || ids.value
  getPeople(uuid).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改人员信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["peopleRef"].validate(valid => {
    if (valid) {
      if (form.value.uuid != null) {
        updatePeople(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPeople(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const uuids = row.uuid || ids.value;
  proxy.$modal.confirm('是否确认删除该人员信息？').then(function() {
    return delPeople(uuids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/people/export', {
    ...queryParams.value
  }, `people_${new Date().getTime()}.xlsx`)
}

getList();

  // 领导弹窗
  const leaderOpen = ref(false);

  // 点击领导弹窗
  function handleClickLeader() {
    leaderOpen.value = true;
  }
  
  // 选择领导
  function handleSelect(data) {
    // 回填数据
    form.value.leaderUuid = data.uuid;
    form.value.leaderName = data.name;
    // 隐藏领导弹窗
    leaderOpen.value = false;
  }
</script>
